package com.flute.framework.util;

import java.io.ByteArrayInputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XMLUtil {

	/**
	 * 将JDOM中的Element转换为xml字符串
	 * @param element 待转换Element
	 * @return xml字符串
	 */
	public static String generateXML(Element element) {
		XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); // 格式化输出，产生缩进和换行
		Format format = outp.getFormat();
		format.setEncoding("UTF-8"); // 设置语言
		format.setExpandEmptyElements(true); // 设置输出空元素为<sample></sample>格式
		outp.setFormat(format);
		return outp.outputString(element);
	}

	/**
	 * 
	 * @param element
	 * @return
	 */
	public static Document getDocument(Element element) {
		String xml = generateXML(element);
		SAXBuilder sb = new SAXBuilder();
		Document doc = null;
		try {
			doc = sb.build(new ByteArrayInputStream(xml.getBytes("UTF-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
}
